package com.leza.myapplication.ui.newfra;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telecom.Call;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.leza.myapplication.R;
import com.leza.myapplication.onenetapi.EntityData;
import com.leza.myapplication.onenetapi.HttpRequest;
import com.leza.myapplication.onenetapi.JsonAnalysis;
import com.leza.myapplication.onenetapi.Token;
import com.leza.myapplication.ui.home.LinearAdapter;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class NewFragment extends Fragment {
    private XRecyclerView xr;
    private List<String> list;
    private Handler mHandler;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_new, container, false);

        //初始化xr
        xr = root.findViewById(R.id.text_new);

        //加布局管理器
        xr.setLayoutManager(new LinearLayoutManager(getActivity()));

        list = new LinkedList<>();
        for (int i = 0; i <= 10; i++) {
            list.add(String.valueOf(i));
        }

//        getData();

        //允许刷新加载
        xr.setPullRefreshEnabled(true);
        xr.setLoadingListener(new XRecyclerView.LoadingListener() {
            //刷新
            @Override
            public void onRefresh() {
//                getData();
                xr.refreshComplete();
            }

            @Override
            public void onLoadMore() {

            }
        });

        return root;
    }

//    //初始化数据
//    @SuppressLint("HandlerLeak")
//    private void getData() {
//        mHandler = new Handler() {
//            @Override
//            public void handleMessage(@NonNull Message msg) {
//                super.handleMessage(msg);
//                Bundle bundle = msg.getData();
//                List<EntityData> entityDataList = (List<EntityData>) bundle.getSerializable("entityDataList");
//
//                for (EntityData i : entityDataList) {
//                    System.out.println(i + "传输成功！");
//                }
//
//                xr.setAdapter(new LinearAdapter(getActivity(), list, entityDataList));
//            }
//        };
//
//        new Thread(new Runnable() {
//            @RequiresApi(api = Build.VERSION_CODES.O)
//            @Override
//            public void run() {
//                Message message = new Message();
//                String tokenStr = Token.CreatToken("349070", "UCsxDg9AkcLK5Me49Hup+pDunajFPVG3inltozXV2A8=");
//                String url = "https://api.heclouds.com/devices/600164009/datapoints";
//                String request =  HttpRequest.sendGet(url, "start=2017-01-01T00:00:00&limit=100");
//                System.out.println(request + "in the Thread");
//                message.what = 1;
//
//                Bundle bundle = new Bundle();
//                bundle.putSerializable("entityDataList", (Serializable) JsonAnalysis.Analysis(request));
//                message.setData(bundle);
//                mHandler.sendMessage(message);
//            }
//        }).start();
//    }
}
